﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

using Core.Common;

namespace DataReceiver.DataAPI
{
    public class ConfigHelper
    {
        public static ConfigHelper Instance = new ConfigHelper();
        private ConfigHelper()
        {
            try
            {
                Config cfg = Serialize.XmlDeserailize<Config>("config");
                if (cfg.Enable == true)
                {
                    cfg.MdAccounts.RemoveAt(0);
                    cfg.Symbols.RemoveAt(0);
                    config = cfg;
                    LogTextHelper.WriteLine("加载配置文件成功");
                }
            }
            catch (Exception e)
            {
                Config config = new Config();
                Serialize.XmlSerialize<Config>("config", config);
                LogTextHelper.WriteLine("加载配置文件失败"+e.Message);
            }
        }

        static Config config;

        public static Model_TdAccount TdAccount
        {
            get
            {
                if (config != null)
                {
                    return config.TdAccount;
                }
                else
                {
                    throw new Exception("读取配置文件失败！");
                }
            }
        }

        public static List<Model_MdAccount> MdAccounts
        {
            get
            {
                if (config != null)
                {
                    return config.MdAccounts;
                }
                else
                {
                    throw new Exception("读取配置文件失败！");
                }
            }
        }

        public static List<string> Symbols
        {
            get
            {
                if (config != null)
                {
                    return config.Symbols;
                }
                else
                {
                    throw new Exception("读取配置文件失败！");
                }
            }
        }

        public static string SqlServerConnectionstring
        {
            get
            {
                if (config != null)
                {
                    return config.SqlServerConnectionstring;
                }
                else
                {
                    throw new Exception("读取配置文件失败！");
                }
            }
        }

        public static string MongoDBConnectionstring
        {
            get
            {
                if (config != null)
                {
                    return config.MongoDBConnectionstring;
                }
                else
                {
                    throw new Exception("读取配置文件失败！");
                }
            }
        }
    }

    [Serializable]
    public class Config
    {
        public Config()
        {
            Enable = false;
            TdAccount = new Model_TdAccount();
            MdAccounts = new List<Model_MdAccount>();
            MdAccounts.Add(new Model_MdAccount());
            Symbols = new List<string>();
            Symbols.Add("合约名");
            SqlServerConnectionstring = "sql连接字符串";
            MongoDBConnectionstring = "mongo连接字符串";
        }

        public bool Enable;
        public Model_TdAccount TdAccount;
        public List<Model_MdAccount> MdAccounts;
        public List<string> Symbols;
        public string SqlServerConnectionstring;
        public string MongoDBConnectionstring;
    }
}
